layui.use(['element', 'table', 'form'], function () {
        var $ = layui.$;
        var table = layui.table;

        getTable();
        function getTable (){
            var courseId=localStorage.getItem("recId");
            console.log(courseId);
            $.ajax({
                url: "/courseTable/getRecord",
                type: "post",
                dataType: "json",
                data: {
                    "courseId": courseId
                },
                success: function (data) {
                    tablereload(data.data);
                }
            })
        }

        function tablereload(data) {
            console.log(data);
            table.render({
                elem: '#table',
                data: data,
                title: '数据列表',
                limit: 10,
                toolbar: '#toptool',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                    first: "首页",
                    last: "尾页",
                },
                size: 'lg',
                cols: [[//定义列
                    {type: 'checkbox'},//复选框靠左
                    {field: 'createDate', title: '审核时间', align: 'center'},
                    {field: "userName", title: "审核人员", align: 'center'},
                    {field: '', title: "审核结果", align: 'center',
                        templet: function (data) {
                            if(data.type==1){
                                var radio = '<p>未通过</p>'
                            }else if(data.type==2){
                                var radio = '<p>已通过</p>'
                            }else{
                                var radio = '<p>未知</p>'
                            }
                            return radio;
                        }
                    },
                    {field: 'remark', title: "备注", align: 'center',},
                ]],
            });
        }
        gCha = function gCha(){
            getTable();
        }

        shangJia = function shangJia(){
            var courseStatus=1;
            var param={
                "courseStatus":courseStatus
            }
            getTable(param);
        }

        weiJia = function weiJia() {
            var courseStatus = 2;
            var param={
                "courseStatus":courseStatus
            }
            getTable(param);
        }

        dShen = function dShen() {
            var remarks="待审核";
            var param={
                "remarks":remarks
            }
            getTable(param);
        }

        Bhui = function Bhui(){
            var remarks="驳回";
            var param={
                "remarks":remarks
            }
            getTable(param);
        }







        //搜索
        search = function search() {
            var productName = $('input[name=productName]').val();
            var courseNprice = $('input[name=courseNprice]').val();

            var classification = document.getElementById("classId");
            var indexClass = classification.selectedIndex;
            var classId = classification.options[indexClass].value;

            // var state = document.getElementById("productSate");
            // var indexState = state.selectedIndex;
            // var productSate = state.options[indexState].value;
            var productLabel = $('input[name=productLabel]').val();
            var param = {
                'coursePrimary':productName,
                'classId':classId,
                "courseNprice":courseNprice
                //'productSate':productSate
            }
            getTable(param);
        }
        //删除商品
        delCourse = function delCourse(id) {
            $.ajax({
                url: '/courseTable/delCourse',
                type: 'post',
                dataType: 'json',
                data: {'id': id},
                success: function (data) {
                    if (data) {
                        layer.msg("删除成功");
                        getTable();
                    } else {
                        layer.msg("删除成功");
                    }
                }
            });
        }
        //查看当前商品的日志
        getCoursejournal = function getCoursejournal(id){
            $.ajax({
                url: "/courseTable/getCoursejournal",
                type: "post",
                dataType: "json",
                data: {"id": id},
                success: function (data) {
                    layer.open({
                        type: 2,
                        title: "详情",
                        area: ['1200px', '700px'],
                        shade: 0,
                        offset: 't',
                        content: "/course/course-see.html",
                        success: function (layero, index) {
                            var obj = layero.find('iframe').contents();
                            obj.find("#productCode").html(data.data.coursePrimary);
                            obj.find("#productName").html(data.data.createDate);

                        }
                    });
                },
            })
        }

        //查看商品详情
        getProduct = function getProduct(id) {
            $.ajax({
                url: "/courseTable/getCourse",
                type: "post",
                dataType: "json",
                data: {"id": id},
                success: function (data) {
                    devicesMsg(data);
                },
            })
        }

        function devicesMsg(data) {
            var userName="";
            var userAcc="";
            console.log("执行页面"+data.data);
            layer.open({
                type: 2,
                title: "详情",
                area: ['1200px', '700px'],
                shade: 0,
                offset: 't',
                content: "/course/course-see.html",
                success: function (layero, index) {
                    $.ajax({
                        url: "/login/getUser",
                        type: "post",
                        dataType: "json",
                        success: function (user) {
                            userName=user.data.trueName;
                            userAcc=user.data.account;
                            console.log(userName,userAcc);
                            var obj = layero.find('iframe').contents();
                            obj.find("#productCode").html(data.data.coursePrimary);
                            obj.find("#productName").html(data.data.createDate);
                            obj.find("#currentpriceProduct").html(data.data.userName);

                            obj.find("#xiaoliang").html(data.data.title);
                            obj.find("#title").html(data.data.courseNprice);

                            obj.find("#productLabel").html(data.data.likeNum);
                            obj.find("#hits").html(data.data.collectNum);
                            obj.find("#productDescription").html(data.data.courseIntro);
                            obj.find("#beizhu").html(data.data.courseInfo);
                            obj.find("#mainImage").attr('src', '/images/goods-img/' + data.data.url);
                            obj.find("#Image").attr('src', '/images/course-img/' + data.data.courseUrl);
                            obj.find("#name").html('用户名：<input type="text"  value="'+userName+'">');
                            obj.find("#acc").html('账号：<input type="text"  value="'+userAcc+'">');

                        },
                    })

                }
            });
        }

        /*表格详情结束*/
        //开关事件
        updateStatus = function updateStatus( id,courseStatus) {
            if(courseStatus==1){
                courseStatus=2;
            }else{
                courseStatus=1;
            }
            $.ajax({
                url: '/courseTable/updateCourseStatus',
                type: 'post',
                dataType: 'json',
                data: {
                    'id': id,
                    'courseStatus': courseStatus
                },
                success:function (data) {
                    layer.msg("修改成功");
                }
            })
        }

        /*表格修改开始*/
        updateProduct = function updateProduct(productId) {
            layer.open({
                type: 2,
                title: "修改",
                area: ['1200px', '700px'],
                shade: 0,
                offset: 't',
                content: "/goods/goods-update.html",
                success: function (layero, index) {
                    var obj = layero.find('iframe').contents();
                    $.ajax({
                        url: "/goodsTable/getProduct",
                        type: "post",
                        dataType: "json",
                        data: {"productId": productId},
                        success: function (data) {
                            console.log(data);
                            obj.find("input[name=productCode]").val(data.data.productCode);
                            obj.find("input[name=productName]").val(data.data.productName);
                            obj.find("input[name=currentpriceProduct]").val(data.data.currentpriceProduct);
                            if (data.data.productSate == 0) {
                                obj.find("#productSate").html("已删除");
                                obj.find("input[name=productSate]").val("已删除");
                            } else if (data.data.productSate == 1) {
                                obj.find("#productSate").html("上架");
                                obj.find("input[name=productSate]").val("上架");
                            } else if (data.data.productSate == 2) {
                                obj.find("#productSate").html("下架");
                                obj.find("input[name=productSate]").val("下架");
                            } else {
                                return "";
                            }
                            obj.find("#productLabel").html(data.data.productLabel);
                            obj.find("#productDescription").html(data.data.productDescription);
                        }
                    });
                }
            });
        }
        /*表格修改结束*/
    }
);

//刷新
function Refresh() {
    location.reload();
}

//跳转
function index() {
    window.location.href = '/course/course-list.html';
}